import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/domain/domain_filter_item_model.dart';
import 'package:juming_test/model/domain/domain_filter_option_model.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_checkbox.dart';

/// 单个输入框组件
class DomainFilterSingleInput extends StatefulWidget {
  const DomainFilterSingleInput({
    super.key,
    required this.model,
    required this.onInputCompleted,
  });

  // 数据源
  final DomainFilterItemModel model;
  // 事件
  // 输入完成回调
  final Function(String) onInputCompleted;

  @override
  State<DomainFilterSingleInput> createState() =>
      _DomainFilterSingleInputState();
}

class _DomainFilterSingleInputState extends State<DomainFilterSingleInput> {
  // 输入框控制器
  late TextEditingController _inputController;
  // 焦点
  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _initInputController();
  }

  @override
  void didUpdateWidget(covariant DomainFilterSingleInput oldWidget) {
    super.didUpdateWidget(oldWidget);
    _inputController.text = widget.model.value;
  }

  @override
  void dispose() {
    _inputController.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.grey[200],
        borderRadius: BorderRadius.circular(4),
      ),
      child: TextField(
        focusNode: _focusNode,
        controller: _inputController,
        onEditingComplete: () {
          _focusNode.unfocus();
          widget.onInputCompleted(_inputController.text);
        },
        decoration: InputDecoration(
          hintText: widget.model.hint,
          border: InputBorder.none,
          contentPadding: EdgeInsets.symmetric(horizontal: 12.w),
        ),
      ),
    );
  }

  // 初始化输入框控制器
  void _initInputController() {
    _inputController = TextEditingController(text: widget.model.value);
    _focusNode = FocusNode();
  }
}
